package com.csun.nusing.ui.activity;

import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.csun.nusing.R;
import com.csun.nusing.presenter.ResignPresenter;
import com.csun.nusing.presenter.inter.IResign;
import com.csun.nusing.ui.BaseActivity;
import com.csun.nusing.util.ConstUtils;
import com.csun.nusing.util.ToastUtil;

import java.util.regex.Pattern;

/**
 * 注册账号界面 ClassName: ResignActivity
 * 
 * @Description: TODO
 * @author fzq
 * @date 2017-10-26
 */
public class ResignActivity extends BaseActivity<ResignPresenter> implements
		IResign {

	private EditText phoneet;
	private EditText phonecodeet;
	private Button codebtn;
	private EditText passwordet;
	private EditText repasswordet;
	private Button comfirmbtn;
	private String phone;
	private String phoneCode;
	private String msgcode;
	private String password;
	private String repassword;
	protected int sixtytime = 60;
	protected Handler handler = new Handler();

	@Override
	public int getContentViewId() {
		return R.layout.activity_resign;
	}

	@Override
	public void initSubViews(View view) {
		super.initSubViews(view);
		findView();
	}

	/**
	 * 初始化控件
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-26
	 */
	private void findView() {
		phoneet = findViewById(R.id.resign_phone_et);
		phonecodeet = findViewById(R.id.resign_phone_code_et);
		codebtn = findViewById(R.id.resign_getphone_code_btn);
		passwordet = findViewById(R.id.resign_password_et);
		repasswordet = findViewById(R.id.resign_repassword_et);
		comfirmbtn = findViewById(R.id.resign_confirm_bt);
		codebtn.setOnClickListener(new click());
		comfirmbtn.setOnClickListener(new click());
	}

	@Override
	public void initData() {
		super.initData();
	}

	/**
	 * 获取验证吗
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-30
	 */
	public void getPhoneCode() {

		phone = phoneet.getText().toString().trim();
		// REGEX_MOBILE_EXACT
		// 正则判断
		if (Pattern.matches(ConstUtils.REGEX_MOBILE_EXACT, phone)) {
			mPresenter.getPhoneCode(this, phone);
			codebtn.setBackgroundResource(R.drawable.notsend_phone_code_icon);
			handler.removeCallbacks(runnable);
			handler.postDelayed(runnable, 1000);
			codebtn.setEnabled(false);
			codebtn.setText("60");
		}
	}

	/**
	 * 注册
	 * 
	 * @Description: TODO
	 * @param
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-30
	 */
	public void resign() {
		if (phoneCode != null) {
			msgcode = phonecodeet.getText().toString().trim();
			Log.e("fzq", "-----msgcode  "+msgcode);
			if (msgcode == null) {
				ToastUtil.showMessage(this, R.string.please_write_message_code);
				return;
			}
			phone = phoneet.getText().toString().trim();

			password = passwordet.getText().toString().trim();
			repassword = repasswordet.getText().toString().trim();
			if (repassword.equals(password)) {
				mPresenter.resign(this, phoneCode, msgcode, password,
						repassword, "2");
			} else {
				ToastUtil.showMessage(this, R.string.psd_is_not_same);
				return;
			}

		} else {
			ToastUtil.showMessage(this, R.string.please_get_phone_code);
		}
	}

	/**
	 * 点击事件 ClassName: click
	 * 
	 * @Description: TODO
	 * @author fzq
	 * @date 2017-10-30
	 */
	private class click implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.resign_getphone_code_btn:
				getPhoneCode();
				break;

			case R.id.resign_confirm_bt:
				resign();
				break;
			}
		}
	}

	@Override
	public void sendCode(String code) {
		phoneCode = code;
	}

	/**
	 * 定时器
	 */
	Runnable runnable = new Runnable() {
		@Override
		public void run() {
			sixtytime--;
			codebtn.setText("" + sixtytime);
			handler.postDelayed(this, 1000);
			if (sixtytime == 0) {
				sixtytime = 60;
				codebtn.setEnabled(true);
				codebtn.setText(R.string.fa_song);
				codebtn.setBackgroundResource(R.drawable.send_phone_code_icon);
				handler.removeCallbacks(runnable);
			}
		}
	};
	
	@Override
	protected ResignPresenter getPresenter() {
		return new ResignPresenter(this);
	}
	

}
